package leetcode.每日一题;

import leetcode.剑指offer.pojo.ListNode;
import org.junit.Test;

/**
 * @author ：zsy
 * @date ：Created 2022/9/17 23:59
 * @description：
 */
public class 删除链表的倒数第N个结点 {
    @Test
    public void test() {

    }

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode curHead = new ListNode(-1);
            curHead.next = head;
            ListNode tmp1 = curHead;
            while (n >= 0) {
                tmp1 = tmp1.next;
                n--;
            }
            ListNode tmp2 = curHead;
            while (tmp1 != null) {
                tmp1 = tmp1.next;
                tmp2 = tmp2.next;
            }
            tmp2.next = tmp2.next.next;
            return curHead.next;
        }
    }
}
